<?php
class TaskService extends Service {
    
    function __construct() {
        parent::__construct();
        $this->mDao = new TaskDao();
    }

    public function insertObject($obj){
        try {
            
            $obj->setCreatedBy($this->userInformation());
            return $this->mDao->insertObject($obj);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }

    public function updateObject($obj,$oldId){
        try {
         
            $obj->setLastUpdatedBy($this->userInformation());
            return $this->mDao->updateObject($obj,$oldId);
            
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }

    public function assignTask($obj){
        try {
            $this->mDao->beginTrans();
            
            $taskAssignmentDao = new TaskAssignmentDao();
            $taskAssignments = $taskAssignmentDao->getListByTaskId($obj->getId());

            if($obj->getTaskAssignment()!=null){
                foreach($obj->getTaskAssignment() as $item){
                    if(($item->getId() == null) || ($item->getId() == '')){
                        if($taskAssignmentDao->insertObject($item)==null) {
                            throw new Exception('Gagal menambahkan assignment project.');
                        }
                    } else {
                        if($taskAssignmentDao->updateObject($item,$item->getId())==null) {
                            throw new Exception('Gagal mengupdate assignment project.');
                        }
                        if (($taskAssignments != null) && (count($taskAssignments) > 0)) {
                            $index_remove = -1;

                            foreach($taskAssignments as $index=>$taskAssignment) {
                                if ($taskAssignment->getId() == $item->getId()) {
                                    $index_remove = $index;
                                    break;
                                }
                            }
                            if ($index_remove > -1) {
                                unset($taskAssignments[$index_remove]);
                            }
                        }
                    }
                }

                if (($taskAssignments != null) && (count($taskAssignments) > 0)) {
                    foreach($taskAssignments as $item) {
                        if (!$taskAssignmentDao->deleteObject($item->getId())) {
                            throw new Exception('Gagal menghapus unused assignment project.');
                        }
                    }
                }
            }
                
            $this->mDao->commitTrans();

            return true;
            
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            $this->mDao->rollback();
        }
    }
    
    public function getList() {
        try {
            return $this->mDao->getList();
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }
    
    public function getObject($id){
        try {
            return $this->mDao->getObject($id);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            throw new Exception($exc->getMessage());
        }
    }

    public function updateStatus($status,$id){
        try {
            return $this->mDao->UpdateStatus($status,$id);
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
        }
    }
    
    public function deleteObject($id){
        try {
            $this->mDao->beginTrans();
            if($this->mDao->deleteObject($id)==false) return false;

            $this->mDao->commitTrans();
            return true;
        } catch (Exception $exc) {
            PageData::addModelError($exc->getMessage());
            $this->mDao->roolback();
            throw new Exception($exc->getMessage());
        }
    }
    
}
?>